﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using LiftBill;
using System.Net;
using LIftBillService;

namespace LiftManagement
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 记账 
        /// 记录人 ,记录当天的  某些人的消费情况  
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            RegistrationEvent();
            LoadData();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }
        ObservableCollection<FoodBase> _FoodBaseList = new ObservableCollection<FoodBase>();

        List<User> _uList = new List<User>();

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LCommon.UserName = Dns.GetHostName();
            tb_User.Text = LCommon.UserName;
            if (_uList == null || _uList.Count == 0)
            {
                User u = new User();
                u.ID = Guid.NewGuid().ToString();
                u.Name = LCommon.UserName;
                Global.UserService.Save(u);
                _uList.Add(u);
            }
            else
            {
                bool isExist = false;
                foreach (User item in _uList)
                {
                    if (item.Name.Equals(item.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        isExist = true;
                    }
                }
                if (!isExist)
                {
                    bool b = Global.UserService.IsExist(LCommon.UserName);
                    if (b)
                    {
                        User u = new User();
                        u.ID = Guid.NewGuid().ToString();
                        u.Name = LCommon.UserName;
                        Global.UserService.Save(u);
                    }
                }
            }
        }

        private void LoadData()
        {
            FoodBase[] foodArry = Global.FoodService.GetAllFoodBaseList();
            _FoodBaseList = new ObservableCollection<FoodBase>(foodArry);
            //。。。。。。。//
            User[] userArry = Global.UserService.GetAllUser();
            _uList = userArry.ToList();
        }

        private void RegistrationEvent()
        {
            btn_Inventory.Click += new RoutedEventHandler(btn_Inventory_Click);
            btn_Add.Click += new RoutedEventHandler(btn_Add_Click);
        }

        void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            AddDishInventoryWin addWin = new AddDishInventoryWin();
            addWin.FoodBaseList = _FoodBaseList;
            addWin.UserList = _uList;
            addWin.Owner = this;
            addWin.Show();
        }

        void btn_Inventory_Click(object sender, RoutedEventArgs e)
        {
            FoodInventoryWin win = new FoodInventoryWin();
            win.FoodBaseList = _FoodBaseList;
            win.Owner = this;
            win.Show();
        }
    }
}
